package com.chidopi.market.aa.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.aa.domain.Functions;

public class FunctionDaoImpl extends AbstractBaseDaoImpl<Functions> {
	private static Logger logger = LoggerFactory.getLogger(FunctionDaoImpl.class);
	public List<Functions> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select * from functions ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select * from functions where " + sqlWhere;
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
		}
	}

	public Functions findById(Object id) {
		String sql = "id=?";
		List<Functions> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	public int insert(Functions t) {
		String sql = "insert into functions (ID, FUNCTIONNAME, ENABLE_FLAG, URL, DISPLAY_ORDER, PARENT_ID, MODULE_ID, CREATE_DATE, CREATE_BY, UPDATE_DATE, UPDATE_BY) "
				+ "values (:ID, :FUNCTIONNAME, :ENABLE_FLAG, :URL, :DISPLAY_ORDER, :PARENT_ID, :MODULE_ID, :CREATE_DATE, :CREATE_BY, :UPDATE_DATE, :UPDATE_BY)";
		return super.execSQL(sql, t);
	}

	public int update(Functions t) {
		String sql = "update functions set FUNCTIONNAME = :FUNCTIONNAME, ENABLE_FLAG = :ENABLE_FLAG, URL = :URL, DISPLAY_ORDER = :DISPLAY_ORDER, PARENT_ID = :PARENT_ID, MODULE_ID = :MODULE_ID, CREATE_DATE = :CREATE_DATE, CREATE_BY = :CREATE_BY, UPDATE_DATE = :UPDATE_DATE, UPDATE_BY = :UPDATE_BY where ID= :ID";
		return super.execSQL(sql, t);
	}

	public int delete(Functions t) {
		String sql = "delete from functions where ID :ID)";
		return super.execSQL(sql, t);
	}

	@Override
	public String getSequenceName() {
		return "Functions_SEQ";
	}

	@Override
	public String getTable() {
		return "Functions";
	}

	@Override
	protected RowMapper<Functions> getRowMapper() {
		return ParameterizedBeanPropertyRowMapper.newInstance(Functions.class);
//		return new ParameterizedRowMapper<Functions>() {
//            public Functions mapRow(ResultSet rs, int rowNum) throws SQLException {
//            	Functions vo = new Functions();
//            	vo.setId(rs.getInt("ID"));
//            	vo.setFunctionname(rs.getString("FUNCTIONNAME"));
//            	vo.setEnable_flag(rs.getString("ENABLE_FLAG"));
//            	vo.setUrl(rs.getString("URL"));
//            	vo.setDisplay_order(rs.getInt("DISPLAY_ORDER"));
//            	vo.setParent_id(rs.getInt("PARENT_ID"));
//            	vo.setModule_id(rs.getInt("MODULE_ID"));
//                vo.setCreate_date(rs.getDate("CREATE_DATE"));
//                vo.setCreate_by(rs.getInt("CREATE_BY"));
//                vo.setUpdate_date(rs.getDate("UPDATE_DATE"));
//                vo.setUpdate_by(rs.getInt("UPDATE_BY"));
//                return vo;
//            }
//        };
	}

}
